package com.example.administrator.capital.mine.second;


import android.app.Fragment;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.presenter.MinePresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.ToastUtil;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * 修改登录密码
 */
public class UpdatePasswordFragment extends BaseBackFragment {

    @BindView(R.id.eidt_password)
    EditText mEidtPassword;
    @BindView(R.id.btn_eye)
    CheckBox mBtnEye;
    @BindView(R.id.eidt_password_new)
    EditText mEidtPasswordNew;
    @BindView(R.id.btn_eye_1)
    CheckBox mBtnEye1;
    @BindView(R.id.eidt_password_sure)
    EditText mEidtPasswordSure;
    @BindView(R.id.btn_eye_2)
    CheckBox mBtnEye2;
    @BindView(R.id.registerInBtn)
    Button mRegisterInBtn;
    Unbinder unbinder;
    private TextView mBack;
    private MinePresenter minePresenter;

    public UpdatePasswordFragment() {
        // Required empty public constructor
    }

    public static UpdatePasswordFragment newInstance() {

        Bundle args = new Bundle();
        UpdatePasswordFragment fragment = new UpdatePasswordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_update_password, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mBack = view.findViewById(R.id.app_title_back);
        isPasswordShowListener();
        minePresenter=new MinePresenter(null);
    }

    private void getDate(){
        minePresenter.updatepassword(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }

            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
            }

            @Override
            public void onSuccess(Response<String> data) {
                ToastUtil.showShort("修改成功");
                _mActivity.onBackPressed();
            }

            @Override
            public void onFail(Response<String> error) {

            }
        },mEidtPassword.getText().toString().trim(),mEidtPasswordNew.getText().toString().trim());
    }
    @Override
    protected void setListener() {
        super.setListener();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();

            }
        });
        mRegisterInBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDate();
            }
        });


    }

    @Override
    protected void setData() {
        super.setData();
    }
    /*显示密码*/
    private void isPasswordShowListener() {
        mBtnEye.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = mEidtPassword.getText().length();
                mEidtPassword.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                mEidtPassword.setSelection(passwordLength);
            }
        });
        mBtnEye1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = mEidtPasswordNew.getText().length();
                mEidtPasswordNew.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                mEidtPasswordNew.setSelection(passwordLength);
            }
        });
        mBtnEye2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = mEidtPasswordSure.getText().length();
                mEidtPasswordSure.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                mEidtPasswordSure.setSelection(passwordLength);
            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
